//////////////////////////////////////////////////////////////////////////
///	\file:	TPView.cpp
///	Copyright (Home) 2010 - All Rights Reserved
///	Author:	Robert.Ma
///	Date:	[2010/01/31]
///	Description:
///		implementation of the CTPView class
//////////////////////////////////////////////////////////////////////////
#include "stdafx.h"
#include "resource.h"
#include <atlmisc.h>

#include "TPView.h"
#include "TPPlayer.h"
#include "TPTrack.h"

#include "TPDefine.h"

BOOL CTPView::PreTranslateMessage(MSG* pMsg)
{
	return IsDialogMessage(pMsg);
}


LRESULT CTPView::OnInitDialog(UINT /*uMsg*/,
							  WPARAM /*wParam*/,
							  LPARAM /*lParam*/,
							  BOOL& /*bHandled*/)
{
	DoDataExchange(FALSE);

	//////////////////////////////////////////////////////////////////////////
	DWORD dwStyleEx = m_wndTrackList.GetExtendedListViewStyle();
	dwStyleEx |= LVS_EX_FULLROWSELECT;
	m_wndTrackList.SetExtendedListViewStyle(dwStyleEx);

	CString strText;
	strText.LoadString(IDS_TRACK);

	m_wndTrackList.AddColumn(strText, 0);

	strText.LoadString(IDS_TITLE);
	m_wndTrackList.AddColumn(strText, 1);

	m_wndTrackList.SetColumnWidth(1, LVSCW_AUTOSIZE_USEHEADER);

	m_wndVolTrack.SubclassWindow(GetDlgItem(IDC_VOLUME));
	m_wndVolTrack.SetParent(*this);
	m_wndVolTrack.SetRange(0, 100, FALSE);

	DWORD	dwVol	= 0L;
	::waveOutGetVolume(NULL, &dwVol);

	DWORD	dwLeft	= LOWORD(dwVol);
	DWORD	dwRight	= HIWORD(dwVol);

	int iPos = (dwLeft + dwRight) / 2 * 100 / USHRT_MAX;
	m_wndVolTrack.SetPos(iPos);

	m_ImageList.Create(IDB_BUTTONS, 20, 6, RGB(255, 0, 255));

	m_wndPrevBtn.AttachToDlgItem(m_hWnd, IDC_PREV);
	m_wndPrevBtn.SetImageList(m_ImageList);
	m_wndPrevBtn.SetImages(0, 1);
	m_wndPrevBtn.SetWindowText(BLANK_STR);
	m_wndPrevBtn.SetBitmapButtonExtendedStyle(BMPBTN_AUTOSIZE | BMPBTN_AUTO3D_SINGLE);

	m_wndPlayBtn.AttachToDlgItem(m_hWnd, IDC_PLAY);
	m_wndPlayBtn.SetImageList(m_ImageList);
	m_wndPlayBtn.SetImages(1, 1);
	m_wndPlayBtn.SetWindowText(BLANK_STR);
	m_wndPlayBtn.SetBitmapButtonExtendedStyle(BMPBTN_AUTOSIZE | BMPBTN_AUTO3D_SINGLE);

	m_wndPauseBtn.AttachToDlgItem(m_hWnd, IDC_PAUSE);
	m_wndPauseBtn.SetImageList(m_ImageList);
	m_wndPauseBtn.SetImages(2, 1);
	m_wndPauseBtn.SetWindowText(BLANK_STR);
	m_wndPauseBtn.SetBitmapButtonExtendedStyle(BMPBTN_AUTOSIZE | BMPBTN_AUTO3D_SINGLE);

	m_wndNextBtn.AttachToDlgItem(m_hWnd, IDC_NEXT);
	m_wndNextBtn.SetImageList(m_ImageList);
	m_wndNextBtn.SetImages(3, 1);
	m_wndNextBtn.SetWindowText(BLANK_STR);
	m_wndNextBtn.SetBitmapButtonExtendedStyle(BMPBTN_AUTOSIZE | BMPBTN_AUTO3D_SINGLE);

	return TRUE;
}

LRESULT CTPView::OnSize(UINT /*uMsg*/, WPARAM /*wParam*/, LPARAM /*lParam*/, BOOL& /*bHandled*/)
{
	if ( ::IsWindow(m_wndTrackList) )
	{
		CRect rcClient;
		GetClientRect(rcClient);

		CRect rcCtrl(rcClient.left + 5,
					 rcClient.top + 10,
					 rcClient.right - 5,
					 rcClient.top + 25);

		m_wndNumStatic.MoveWindow(rcCtrl);

		rcCtrl.top		= rcClient.top + 30;
		rcCtrl.bottom	= rcClient.bottom - 35;

		m_wndTrackList.MoveWindow(rcCtrl);

		rcCtrl.top		= rcCtrl.bottom + 5;
		rcCtrl.bottom	= rcClient.bottom - 10;
		rcCtrl.right	= rcCtrl.left + 20;
		m_wndPrevBtn.MoveWindow(rcCtrl);

		rcCtrl.left		= rcCtrl.right + 5;
		rcCtrl.right	= rcCtrl.left + 20;
		m_wndPlayBtn.MoveWindow(rcCtrl);

		rcCtrl.left		= rcCtrl.right + 5;
		rcCtrl.right	= rcCtrl.left + 20;
		m_wndPauseBtn.MoveWindow(rcCtrl);

		rcCtrl.left		= rcCtrl.right + 5;
		rcCtrl.right	= rcCtrl.left + 20;
		m_wndNextBtn.MoveWindow(rcCtrl);

		rcCtrl.left		= rcCtrl.right + 15;
		rcCtrl.right	= rcClient.right - 5;
		m_wndVolTrack.MoveWindow(rcCtrl);
	}

	return 0;
}

LRESULT CTPView::OnNMDblclkTrackList(int /*idCtrl*/, LPNMHDR /*pNMHDR*/, BOOL& /*bHandled*/)
{
	::SendMessage(GetParent(), WM_COMMAND, ID_ACTION, (LPARAM)m_hWnd);

	return 0;
}

void CTPView::UpdateList()
{
	if ( ::IsWindow(m_wndTrackList) )
	{
		int iCount = CTPPlayer::GetTrackCount();

		if ( 0 < iCount )
		{
			m_wndTrackList.DeleteAllItems();

			const TRACK* pTrack = NULL;
			CString strIndex;

			for ( int i = 0; i < iCount; ++i )
			{
				pTrack = CTPPlayer::GetTrackByIndex(i);

				strIndex.Format(_T("%d"), i + 1);
				m_wndTrackList.InsertItem(i, strIndex);
				m_wndTrackList.SetItemData(i, (DWORD_PTR)((TRACK*)pTrack));
				m_wndTrackList.AddItem(i, 1, pTrack->sTrackTitle);
			}
		}
	}
}

void CTPView::UpdateTime()
{
	if ( ::IsWindow(m_wndNumStatic) )
	{
		const int iPlayTime = CTPPlayer::GetPlayTime() / uiSeconds;
		const TRACK* pCurTrack = NULL;
		int iTrack = -1;

		if ( CTPPlayer::GetTrack(iPlayTime, iTrack, &pCurTrack) )
		{
			const int	iTrackTime	= iPlayTime - pCurTrack->index.wIndex;
			const int	iMinutes	= iTrackTime / 60;
			const int	iSec		= iTrackTime % 60;

			CString strFormat, strTime;
			strFormat.LoadString(IDS_TRACK_INFO);
			strTime.Format(strFormat, iTrack + 1, iMinutes, iSec);

			m_wndNumStatic.SetWindowText(strTime);
		}
	}
}

int CTPView::GetCurTrack() const
{
	int iTrack = -1;

	if ( ::IsWindow(m_wndTrackList) )
	{
		iTrack = m_wndTrackList.GetSelectedIndex();
	}

	return iTrack;
}

LRESULT CTPView::OnBnClickedPause(WORD /*wNotifyCode*/,
								  WORD /*wID*/,
								  HWND /*hWndCtl*/,
								  BOOL& /*bHandled*/)
{
	::SendMessage(GetParent(), WM_COMMAND, ID_PLAY_PAUSE, (LPARAM)m_hWnd);

	return 0;
}

LRESULT CTPView::OnBnClickedNext(WORD /*wNotifyCode*/,
								 WORD /*wID*/,
								 HWND /*hWndCtl*/,
								 BOOL& /*bHandled*/)
{
	::SendMessage(GetParent(), WM_COMMAND, ID_PLAY_NEXT, (LPARAM)m_hWnd);

	return 0;
}

LRESULT CTPView::OnBnClickedPlay(WORD /*wNotifyCode*/,
								 WORD /*wID*/,
								 HWND /*hWndCtl*/,
								 BOOL& /*bHandled*/)
{
	::SendMessage(GetParent(), WM_COMMAND, ID_PLAY_PLAY, (LPARAM)m_hWnd);

	return 0;
}

LRESULT CTPView::OnBnClickedPrev(WORD /*wNotifyCode*/,
								 WORD /*wID*/,
								 HWND /*hWndCtl*/,
								 BOOL& /*bHandled*/)
{
	::SendMessage(GetParent(), WM_COMMAND, ID_PLAY_PREV, (LPARAM)m_hWnd);

	return 0;
}
